﻿using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Edge;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.UI;
using SuiFang3;

Random ran = new Random();

Console.WriteLine("开始读取文件....");

var curDir = Directory.GetCurrentDirectory();
var fileDir = $"{curDir}\\File\\data.xlsx";

Console.WriteLine($"文件路径         {fileDir}");

if (!File.Exists(fileDir))
{
    Console.WriteLine("文件不存在");
    return;
}

IWorkbook workbook;

using (var fs = new FileStream(fileDir, FileMode.Open, FileAccess.Read, FileShare.Read))
{
    workbook = new XSSFWorkbook(fs);
}

var people = new List<Person>();

for (int sheetIndex = 0; sheetIndex < workbook.NumberOfSheets; sheetIndex++)
{
    var sheet = workbook.GetSheetAt(sheetIndex);

    switch (sheetIndex)
    {
        case 0:
            for (int rowIndex = 1; rowIndex < sheet.LastRowNum; rowIndex++)
            {
                var item = new Person();
                var row = sheet.GetRow(rowIndex);

                for (int cellIndex = 0; cellIndex < row.LastCellNum; cellIndex++)
                {
                    var cell = row.GetCell(cellIndex);

                    switch (cellIndex)
                    {
                        case 0:
                            item.ID = cell.ToString();
                            break;

                        case 1:
                            item.Name = cell.ToString();
                            break;
                        
                        case 2:
                            item.Workplace = cell.ToString();
                      
                            break;
                        case 3:
                            item.ContactPhoneNumber = cell.ToString();
                            break;
                        case 4:
                            item.ContactName = cell.ToString();
                            break;
                        case 5:
                            item.Phone = cell.ToString();
                            break;
                        case 6:
                            item.ResidenceType = cell.ToString();
                            break;
                        case 7:
                            item.Address = cell.ToString();
                            break;
                        case 8:
                            item.ResidenceAddress = cell.ToString();
                            break;
                        case 9:
                            item.Creator =cell.ToString();
                            break;
                        case 10:
                            item.Doctor = cell.ToString();
                            break;
                        default:
                            break;
                    }
                }

                people.Add(item);
            }
            break;
        default:
            break;
    }
}

IWebDriver driver = null;
bool canCopy = false;

driver = new EdgeDriver();
//driver = new ChromeDriver(@$"{curDir}\File\chromedriver.exe");
var wait = new WebDriverWait(driver, TimeSpan.FromDays(1));

driver.Navigate().GoToUrl("http://111.40.175.136:18081");
// 登录
driver.FindElement(By.Id("InstituteCode")).SendKeys("100190");

driver.FindElement(By.Id("UserCode")).SendKeys("admin21");

driver.FindElement(By.Id("UserPass")).SendKeys("k84621548.@");

driver.FindElement(By.Id("btn_login")).Click();

Thread.Sleep(1000);

wait.Until(q => {

    try
    {
        if (q.FindElement(By.Id("hidInstituteName")) != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    catch (Exception)
    {
        return false;
    }
});

driver.Manage().Window.Maximize();

// 个人健康档案
driver.FindElements(By.CssSelector(".m-subnav li"))[0].FindElement(By.TagName("A")).Click();

wait.Until(q => q.FindElement(By.ClassName("m-iframe")));

// 进入健康档案
driver.SwitchTo().Frame(1);

try
{
    foreach (var item in people)
    {
        var searchInput =  driver.FindElement(By.Id("SearchKey"));
        searchInput.Clear();
        searchInput.SendKeys(item.ID);

        Thread.Sleep(2000);

        driver.FindElement(RelativeBy.WithLocator(By.TagName("a")).RightOf(searchInput)).Click();

        wait.Until(q => q.FindElement(By.ClassName("blueLink")));

        Thread.Sleep(2000);

        driver.FindElement(By.ClassName("blueLink")).Click();

        ChangeRecord( wait, item);
    }
}
catch (Exception ex)
{
    Console.WriteLine(ex);
}

/// <summary>
/// 点击个人健康档案列表开始修改随访记录
/// </summary>
/// <param name="item"></param>+
/// <param name="wait"></param>
void ChangeRecord( WebDriverWait wait, Person person)
{

    Thread.Sleep(1000);

    // 打开病人档案 跳转到新开页面
    driver.SwitchTo().Window(driver.WindowHandles.Last());
    // 进入个人基本信息
    driver.SwitchTo().Frame(0);

    ChangeBaseInfo(driver, wait, person);

    BackWindow(driver);

    return ;
}

/// <summary>
/// 高血压表单逻辑
/// </summary>
/// <param name="gaoXueYa"></param>
/// <param name="wait"></param>
/// <param name="formData"></param>
void ChangeBaseInfo(IWebDriver driver, WebDriverWait wait, Person person)
{
    driver.Manage().Window.Size = new System.Drawing.Size(1024, 768);
    driver.Manage().Window.FullScreen();
    Thread.Sleep(2000);

    // 输入姓名
    InputStr(driver.FindElement(By.Id("_easyui_textbox_input3")), person.Name);
    // 工作单位
    InputStr(driver.FindElement(By.Id("_easyui_textbox_input5")), person.Workplace);
    // 联系电话
    InputStr(driver.FindElement(By.Id("_easyui_textbox_input8")), person.Phone);
    // 联系人姓名
    InputStr(driver.FindElement(By.Id("_easyui_textbox_input7")), person.ContactName);
    // 责任医生
    InputStr(driver.FindElement(By.Id("_easyui_textbox_input88")), person.Doctor);
    // 现住址
    InputStr(driver.FindElement(By.Id("_easyui_textbox_input9")), person.Address);
    // 常住类型
    InputStr(driver.FindElement(By.Id("_easyui_textbox_input47")), person.ResidenceType);
    // 联系电话
    InputStr(driver.FindElement(By.Id("_easyui_textbox_input6")), person.ContactPhoneNumber);
    // 户籍地址
    InputStr(driver.FindElement(By.Id("_easyui_textbox_input10")), person.ResidenceAddress);
    // 建档人
    InputStr(driver.FindElement(By.Id("_easyui_textbox_input89")), person.Creator);

    // 保存随访记录
    driver.FindElement(By.ClassName("fsave")).Click();

    Thread.Sleep(3000);

    // 点击保存成功框
    wait.Until(q => q.FindElement(By.ClassName("messager-window")));
    driver.FindElements(By.ClassName("l-btn-text")).First(q => q.Text == "确定").Click();

    Thread.Sleep(1000);

    return;
}

/// <summary>
/// 完成随访填写流程返回主页面
/// </summary>
/// <param name="driver"></param>
void BackWindow(IWebDriver driver)
{
    // 关闭窗口返回第一页签
    driver.Close();
    driver.SwitchTo().Window(driver.WindowHandles[0]);
    driver.SwitchTo().Frame(1);
}

/// <summary>
/// 清空输入框录入文本
/// </summary>
/// <param name="input"></param>
/// <param name="target"></param>
void InputStr(IWebElement input, string target)
{
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace); 
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);
    input.SendKeys(Keys.Backspace);

    input.SendKeys(target);
}

/// <summary>
/// 检查是否控制不满意
/// </summary>
/// <param name="driver"></param>
/// <returns></returns>
bool WillDo(IWebDriver driver, bool isTang)
{
    if (isTang)
    {
        var date = driver.FindElement(RelativeBy.WithLocator(By.TagName("input")).Below(driver.FindElement(By.Id("_easyui_textbox_input48")))).GetAttribute("value");
        return DateTime.Parse(date).AddDays(5) >= DateTime.Today;
    }
    else
    {
        var date = driver.FindElement(RelativeBy.WithLocator(By.TagName("input")).Below(driver.FindElement(By.Id("_easyui_textbox_input37")))).GetAttribute("value");
        return DateTime.Parse(date).AddDays(5) >= DateTime.Today;
    }
}
